package com;

import entity.Emp;
import entity.ListVO;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Controller

public class HomeController {

    // 的确是单利的
    public HomeController() {
        System.out.println("-------------");
    }

    //spring比较新的版本是可以得到方法的参数名字的
    //简单类型不能为null，所以一定要给它传一个值过来
    @RequestMapping("/simple")
    public String simple(int a) {
        // int double,float
        //简单类型不能赋值为null
        System.out.println("simple---" + a);
        return "index";
    }

    @RequestMapping("/wrapper")
    public String wrapper(Integer b) {

        System.out.println("wrapper---" + b);
        return "index";
    }

    @RequestMapping("/complex")
    public String complex(Emp emp) {

        System.out.println("complex---" + emp);
        return "index";
    }

    @RequestMapping("/d")
    public String d(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        System.out.println("date---" + date);
        return "index";
    }


    @RequestMapping("/d2")
    public String d2(Date date) {

        System.out.println("date2---" + date);
        return "index";
    }

    // http://localhost:8080/array?hobby=a,b,c
    // http://localhost:8080/array?hobby=aaa&hobby=bb
    @RequestMapping("/array")
    public String array(String[] hobby) {

        System.out.println("length:" + hobby.length);
        System.out.println("hobby---" + Arrays.toString(hobby));
        return "index";
    }

    // 不能象下面这样写来接收list数据，spring mvc不支持
   /* @RequestMapping("/list")
    public String list( List hobby) {

        System.out.println("size:" + hobby.size());
        hobby.forEach(System.out::println);
        return "index";
    }*/

    //地址类似于数组案例的格式
    @RequestMapping("/list")
    public String list(ListVO vo) {

        System.out.println("size:" + vo.getHobby().size());
        vo.getHobby().forEach(System.out::println);
        return "index";
    }


    //http://localhost:8080/list2?emps[0].id=1&emps[0].name=a&emps[1].id=2&emps[1].name=b
    @RequestMapping("/list2")
    public String list2(ListVO vo) {

        System.out.println("size:" + vo.getEmps().size());
        vo.getEmps().forEach(System.out::println);
        return "index";
    }

    //象HttpServletRequest，HttpServletResponse，HttpSession等特殊类型，
    // springmvc会自动注入
    @RequestMapping("/teshu")
    public String teShu(HttpServletRequest request) {
        //request.getParameter()

        System.out.println(request);
        return "index";
    }


    //rp?b=200
    @RequestMapping("/rp")
    public String requestParam(@RequestParam(value = "b", required = false, defaultValue = "100") int a) {
        System.out.println("a:" + a);
        return "index";


    }

    @RequestMapping("/page{pageno}")
    public String pathPariable(int pageno) {
        System.out.println("pageno:" + pageno);
        return "index";
    }
}
